﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class AuthorizeCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetUnauthorizedCustomerList();
            }
        }

        protected void btnAuthorize_Click(object sender, EventArgs e)
        {
            AuthorizeCustomers();
        }

        protected void grdAuthList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdAuthList.PageIndex = e.NewPageIndex;
            GetUnauthorizedCustomerList();
        }

        protected void grdAuthList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
        }

        private void GetUnauthorizedCustomerList()
        {
            try
            {
                List<Customer> dtAuthList = (new CustomerEntity()).GetUnauthorizedCustomers(((User)Session["CurrentUserVO"]).UserID);
                grdAuthList.DataSource = dtAuthList;
                grdAuthList.DataBind();
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    throw;
                }
            }
        }

        private void AuthorizeCustomers()
        {
            try
            {
                int count = 0;
                StringBuilder checkedList = new StringBuilder();
                foreach (System.Web.UI.WebControls.GridViewRow dgi in grdAuthList.Rows)
                {
                    CheckBox chkSelect = (CheckBox)dgi.Cells[0].FindControl("chkSelect");
                    if (chkSelect.Checked)
                    {
                        checkedList.Append("'" + dgi.Cells[1].Text.ToString() + "'" + ",");
                    }
                }

                //if customers selected then authorize
                if (checkedList.Length >= 0)
                {
                    checkedList.Remove(checkedList.Length - 1, 1);
                    count = (new CustomerEntity()).AuthorizeCustomers(((User)Session["CurrentUserVO"]).UserID, checkedList.ToString());

                    //if count > 0 then success
                    if (count > 0)
                    {
                        lblMessage.Text = "Successfully Authorised (" + count + ") Customer!";
                        btnClickPopup.Show();
                    }
                    else
                    {
                        lblMessage.Text = "Failed to Authorise selected Customer(s)!";
                        btnClickPopup.Show();
                    }
                }
                else
                {
                    lblMessage.Text = "Please select Customer(s) to authorize!";
                    btnClickPopup.Show();
                }

                //refresh unauthorized customer list
                GetUnauthorizedCustomerList();
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    throw;
                }
            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }
    }
}